package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SchoolStudentjk;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISchoolStudentjkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SchoolStudentjkServiceImpl implements ISchoolStudentjkService {
    @Autowired
    private SchoolStudentjkMapper schoolStudentjkMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public SchoolStudentjk selectSchoolStudentjkById(Long id) {
        return schoolStudentjkMapper.selectSchoolStudentjkById(id);
    }

    @Override
    public List<SchoolStudentjk> selectSchoolStudentjkList(SchoolStudentjk schoolStudent) {
        return schoolStudentjkMapper.selectSchoolStudentjkList(schoolStudent);
    }

    @Override
    public int insertSchoolStudentjk(SchoolStudentjk schoolStudent) {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        schoolStudent.setTeacherId(userId);
        schoolStudent.setCreateTime(DateUtils.getNowDate());
        return schoolStudentjkMapper.insertSchoolStudentjk(schoolStudent);
    }

    @Override
    public int updateSchoolStudentjk(SchoolStudentjk schoolStudent) {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        schoolStudent.setTeacherId(userId);
        return schoolStudentjkMapper.updateSchoolStudentjk(schoolStudent);
    }

    @Override
    public int updateSchoolStudentjkAll(List<SchoolStudentjk> list) {
        SchoolStudentjk item = list.get(0);
        schoolStudentjkMapper.deleteSchoolStudentjkByClassesId(item.getClassesId());
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        SysPost info = postMapper.getPostCodeUnique("xuesheng");
        for (SchoolStudentjk obj:list) {
            obj.setTeacherId(userId);
            obj.setCreateTime(DateUtils.getNowDate());
            schoolStudentjkMapper.insertSchoolStudentjk(obj);
            userPostMapper.deleteUserPostByUserId(obj.getStudentId());
            List<SysUserPost> postList = new ArrayList<SysUserPost>();
            SysUserPost up = new SysUserPost();
            up.setUserId(obj.getStudentId());
            up.setPostId(info.getPostId());
            postList.add(up);
            userPostMapper.batchUserPost(postList);
            SysUser user = new SysUser();
            user.setUserId(obj.getStudentId());
            user.setDeptId(obj.getSchoolId());
            userMapper.updateUser(user);
        }
        return list.size();
    }

    @Override
    public int deleteSchoolStudentjkByIds(Long[] ids) {
        return schoolStudentjkMapper.deleteSchoolStudentjkByIds(ids);
    }

    @Override
    public int deleteSchoolStudentjkById(Long id) {
        return schoolStudentjkMapper.deleteSchoolStudentjkById(id);
    }
}
